﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Arcadium.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Arcadium.Screens.Menus.Sidebar
{
    class SidebarDropdownListView<T>
    {
        private SidebarDropdownList<T> list;

        #region Field

        private Vector2 startPosition;
        private Texture2D mainTexture;
        private Texture2D itemTexture;
        private Texture2D item_selectedTexture;

        private const byte dropdownIndentX = 102;

        #endregion

        public SidebarDropdownListView(SidebarDropdownList<T> list, Vector2 startPosition)
        {
            this.startPosition = startPosition;
            this.list = list;
        }

        public void LoadContent(ContentManager content)
        {
            mainTexture = content.Load<Texture2D>("MenuSidebar\\dropdown_main");
            itemTexture = content.Load<Texture2D>("MenuSidebar\\dropdown_item");
            item_selectedTexture = content.Load<Texture2D>("MenuSidebar\\dropdown_item_selected");
        }

        public void Draw(SpriteBatch spriteBatch, bool sidebarHasFocus)
        {
            // Dropdown main texture
            spriteBatch.Draw(mainTexture,
                View.Adjust(startPosition), // Position
                null, // Src Rect 
                list.IsSelected && sidebarHasFocus ? Color.Gray : Color.White,
                0, // Rotation 
                Vector2.Zero, // Origin
                View.ScreenScale,
                SpriteEffects.None,
                list.LockedFocus ? 0 : .6f); // Depthlayer

            if (list.LockedFocus)
            {
                for (int i = 0; i < list.Items.Count; i++)
                {
                    // Background
                    spriteBatch.Draw(itemTexture,
                        View.Adjust(startPosition + new Vector2(dropdownIndentX, (i + 1) * mainTexture.Height)),
                        null,
                        list.Items[i].Equals(list.SelectedItem) ? Color.Gray : Color.White,
                        0, // Rotation
                        Vector2.Zero, // Origin
                        View.ScreenScale,
                        SpriteEffects.None,
                        .25f);
                    // Text
                    spriteBatch.DrawString(ViewResource.MenuFont, list.Items[i].ToString(),
                        View.Adjust(startPosition + new Vector2(dropdownIndentX + ViewResource.TextIndent, (i + 1.5f) * mainTexture.Height)),
                        list.Items[i].Equals(list.SelectedItem) ? Color.Red : Color.White, 0,
                        new Vector2(0, ViewResource.MenuFont.LineSpacing / 2),
                        View.ScreenScale, SpriteEffects.None, 0);
                }
            }
            else
            {
                // Text
                spriteBatch.DrawString(ViewResource.MenuFont, list.SelectedItem.ToString(),
                    View.Adjust(startPosition + new Vector2(dropdownIndentX + ViewResource.TextIndent, mainTexture.Height / 2)),
                    list.IsSelected && sidebarHasFocus ? Color.Red : Color.White, 0,
                    new Vector2(0, ViewResource.MenuFont.LineSpacing / 2),
                    View.ScreenScale, SpriteEffects.None, .5f);
            }
        }

    }
}
